<template>
	<view>
		<page-head :title="id == '' ? '添加企业':'修改企业信息'" :headtype="2"></page-head>
		<view class="fromblock">
			<view class="formitem marginAuto">
				<view class="formitem-title"><text class="bt">*</text> 企业名称</view>
				<view class="formitem-msg" style="background:#fff;">
					<input class="msginput" type="text" v-model="companyname" placeholder="请输入企业名称"/>
				</view>
			</view>
			<view class="formitem marginAuto">
				<view class="formitem-title"><text class="bt">*</text> 企业类型</view>
				<view class="formitem-msg flex" style="background:#fff;">
					<view class="compantypeeach flex" @click="companytypeg=!companytypeg">
						<view class="companticon">
							<uv-icon name="checkmark-circle-fill" :color="companytypeg ? '#2340FF':'#999999'" size="20"></uv-icon>
						</view>
						<view class="companttitle">工业类型</view>
					</view>
					<view class="compantypeeach flex" @click="companytypen=!companytypen">
						<view class="companticon">
							<uv-icon name="checkmark-circle-fill" :color="companytypen ? '#2340FF':'#999999'" size="20"></uv-icon>
						</view>
						<view class="companttitle">农业类型</view>
					</view>
				</view>
			</view>
			<view class="formitem marginAuto">
				<view class="formitem-title"><text class="bt">*</text> 营业执照</view>
				<view class="formitem-msg">
					<view class="upload flex" @click="uploadfile('business')">
						<view class="icon marginAuto">
							<uv-icon name="plus" color="#CECECE" size="22"></uv-icon>
						</view>
						<image class="uploadimg" :src="apifun.URLimg+business" mode=""></image>
					</view>
				</view>
			</view>
			<view class="formitem marginAuto">
				<view class="formitem-title flex">
					<text class="bt">*</text>
					<text> 授权书盖章上传</text>
					<text class="mainColor" style="margin-left:10rpx;" @click="mbdownfun">模板下载</text>
					<image style="width:30rpx;height:30rpx;margin-top:5rpx;margin-left:10rpx;" src="@/static/icon/download.png" mode=""></image>
				</view>
				<view class="formitem-msg">
					<view class="upload flex" @click="uploadfile('sqs')">
						<view class="icon marginAuto">
							<uv-icon name="plus" color="#CECECE" size="22"></uv-icon>
						</view>
						<image class="uploadimg" :src="apifun.URLimg+sqs" mode=""></image>
					</view>
				</view>
			</view>
			<!-- <view class="formitem marginAuto">
				<view class="formitem-title"><text class="bt">*</text> 开票信息</view>
				<view class="formitem-msg" style="background:#fff;" @click="apifun.navigate('/pages/usermy/invoice')">
					<view class="invoicemsg flex">
						<view v-if="!addmyinvoice.invoice_type">请设置开票信息</view>
						<view v-if="addmyinvoice.invoice_type">已设置开票信息</view>
						<view class="icon">
							<uv-icon name="arrow-right" color="#999999" size="20"></uv-icon>
						</view>
					</view>
				</view>
			</view> -->
		</view>
		<view class="marginAuto" style="width:600rpx;margin-top:100rpx;">
			<uv-button :text="id==''? '确认添加':'确认修改'" @click="enter()" :customStyle="{
				'background':'#2340FF',
				'color':'#fff',
				'font-size':'30rpx',
				'width':'600rpx',
				'height':'100rpx',
				'border-radius':'20rpx'
			}"></uv-button>
		</view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				apifun:this.apifun,
				id:'',
				isagreement:true,
				companyname:'',
				companytypeg:false,
				companytypen:false,
				business:'',
				sqs:'',
				istime: false,
				times:60,
				veri:'',
				pass:'',
				enterpass:'',
				addmyinvoice:{}
			};
		},
		onLoad(option) {
			this.id = option.id || '';
			if(!option.id){
				uni.removeStorageSync('addmyinvoice');
			}else{
				this.getbusinmsgfun();
			}
		},
		onShow() {
			this.addmyinvoice = uni.getStorageSync('addmyinvoice') || {};
		},
		methods: {
			enter(){
				let companytype = [];
				if(this.companytypeg){
					companytype.push(1)
				};
				if(this.companytypen){
					companytype.push(2)
				};
				let addmyinvoice = uni.getStorageSync('addmyinvoice') || {};
				this.addmyinvoice = addmyinvoice;
				let sendData = {
					company_name:this.companyname,
					company_type:companytype,
					business_img:this.business,
					authorization:this.sqs,
					invoice_type:addmyinvoice.invoice_type || '',
					invoice_title:addmyinvoice.invoice_title || '',
					invoice_tax:addmyinvoice.invoice_tax || '',
					reg_address:addmyinvoice.reg_address || '',
					reg_tel:addmyinvoice.reg_tel || '',
					bank_name:addmyinvoice.bank_name || '',
					bank_account:addmyinvoice.bank_account || ''
				}
				// if(!addmyinvoice.invoice_type){
				// 	this.apifun.toast('请输入发票信息')
				// 	return false
				// };
				if(this.companyname == '' || this.business=='' || this.sqs== ''){
					this.apifun.toast('请将信息补充完整')
					return false
				};
				if(companytype.length <= 0){
					this.apifun.toast('请选择企业类型')
					return false
				};
				let urls = '/user/company/addSubCompany';
				if(this.id){
					sendData.id = this.id;
					urls = '/user/company/editCompanyInfo';
				}
				this.apifun.unirequest(urls,'post',sendData,(res)=>{
					if(res.code === 200){
						let datas = res.data;
						this.apifun.toast('操作成功');
						setTimeout(()=>{
							this.apifun.goback()
						},1500)
					}else{
						this.apifun.toast(res.msg)
					}
				})
			},
			getbusinmsgfun(){
				this.apifun.unirequest('/user/company/getCompanyInfo','post',{
					company_id:this.id
				},(res)=>{
					if(res.code === 200){
						let datas = res.data;
						this.addmyinvoice = datas;
						this.companyname = datas.company_name;
						this.companytypeg = datas.company_type.includes('1');
						this.companytypen = datas.company_type.includes('2');
						this.business = datas.business_img;
						this.sqs = datas.authorization;
						let sendData = {
							invoice_type:datas.invoice_type,
							invoice_title:datas.invoice_title,
							invoice_tax:datas.invoice_tax,
							reg_address:datas.reg_address,
							reg_tel:datas.reg_tel,
							bank_name:datas.bank_name,
							bank_account:datas.bank_account
						}
						uni.setStorageSync('addmyinvoice', sendData);
					}else{
						this.apifun.toast(res.msg)
					}
				})
			},
			uploadfile(name){
				if(name == 'sqs'){
					this.apifun.uploadFileimg((res)=>{
						if(res.code == 200){
							this[name] = res.data.src
						}else{
							this.apifun.toast(res.msg)
						}
					},this,{type:110})
				}else{
					this.apifun.uploadFileimg((res)=>{
						if(res.code == 200){
							this[name] = res.data.src
						}else{
							this.apifun.toast(res.msg)
						}
					},this)
				}
			},
			
			mbdownfun(){
				let that = this;
				this.apifun.unirequest('/common/index/template','post',{},(res)=>{
					if(res.code === 200){
						let datas = res.data;
						uni.downloadFile({
							url:this.apifun.URLimg+datas,
							success: (res) => {
								if (res.statusCode === 200) {
									let downflie = res.tempFilePath
									uni.openDocument({
										filePath:downflie,
										showMenu:true,
										success:function(res){}
									})
									// uni.saveFile({  // wx.getFileSystemManager().saveFile
									// 	tempFilePath:downflie,
									// 	success: function (ress) {
									// 		console.log(88,ress)
									// 		var savedFilePath = ress.savedFilePath;  // 保存路径
									// 		that.apifun.toast('文件已保存至'+savedFilePath);
									// 	}
									// })
								}
							}
						});
					}else{
						this.apifun.toast(res.msg)
					}
				})
			},
			vericlick() {
				let that = this;
				let flag = true;
				if (that.apifun.testphone.test(that.phone) == false || that.phone == '') {
					that.apifun.toast("手机格式有误");
					return false
				};
				if (flag) {
					flag = false;
					let index = 60;
					that.istime = true;
					let time = setInterval(function() {
						if (index > 0) {
							--index;
							that.times = index;
						} else {
							index = 60;
							clearInterval(time)
							that.istime = false;
							flag = true;
						}
					}, 1000);
				}
				let sendData = {
					mobile: that.phone,
					type: '1'
				}
				that.apifun.unirequest(this.apifun.login_send_code, 'post', sendData, (res) => {
					if (res.code === 200) {
						let data = res.data;
						that.apifun.toast('发送成功；请注意查收');
					} else {
						that.apifun.toast(res.msg)
					}
				},false,true)
			},
		},
	};
</script>

<style scoped lang="scss">
	page{
		background-color: #FBFBFB;
		padding-bottom: 100rpx;
	}
	.fromblock{
		.formitem{
			width:90%;
			.formitem-title{
				font-size: 30rpx;
				color: #333333;
				.bt{
					color:#E51818;
				}
			}
			.formitem-msg{
				flex: 1;
				border-radius:10rpx;
				padding: 0 40rpx;
				margin: 20rpx auto;
				.msginput{
					flex: 1;
					height:100rpx;
					line-height: 100rpx;
					font-size: 30rpx;
					color: #333;
				}
				.compantypeeach{
					line-height: 100rpx;
					margin-right:100rpx;
					.companticon{
						margin-top:28rpx;
						margin-right: 10rpx;
					}
					.companttitle{
						font-size: 30rpx;
						color: #999999;
					}
				}
				.upload{
					position:relative;
					width: 200rpx;
					height: 200rpx;
					border-radius:10rpx;
					background-color: #F3F3F3;
					.icon{
						
					}
					.uploadimg{
						position: absolute;
						left: 0;
						top: 0;
						z-index: 2;
						width: 200rpx;
						height: 200rpx;
						border-radius:10rpx;
					}
				}
				.invoicemsg{
					line-height: 100rpx;
					color:#999999;
					justify-content: space-between;
					.icon{
						margin-top:30rpx;
					}
				}
			}
		}
	}
</style>